package cn.lwj.wsClient.control;


import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.context.ServletContextAware;

@SuppressWarnings({"unchecked", "serial", "rawtypes"})
@Component
//@Component(value = "fzbabaseControl")
public class BaseControl implements ServletContextAware {
    public static final String LOGIN_LOGINSESSION_KEY = "courtInfo";
    public Logger log = Logger.getLogger(this.getClass());

    protected HttpSession session;
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected ServletContext servletContext;

    public String getRequestValue(String name) {
        return getRequest().getParameter(name) == null || getRequest().getParameter(name).trim().equals("") ? "" : getRequest().getParameter(name).trim();
    }

    public String[] getRequestValues(String name) {
        return getRequest().getParameterValues(name) == null || getRequest().getParameterValues(name).equals("") ? new String[0] : getRequest().getParameterValues(name);
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }

    /**
     * 对servletAPI进行赋值  这个方法必须要
     * 被@ModelAttribute注释的方法会在此controller每个方法执行前被执行
     *
     * @param req
     * @param rep
     * @param session
     * @author
     */
    @ModelAttribute
    public void initServletAPI(HttpServletRequest req, HttpServletResponse rep, HttpSession session) {
        this.request = req;
        this.response = rep;
        this.session = session;
    }
    /**
     * 摧毁SESSION
     */
    /*public void removeSessionUser() {
        getSession().removeAttribute(LOGIN_LOGINSESSION_KEY);
    }

    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    public void setServletResponse(HttpServletResponse response) {
        this.response = response;
    }



    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    public HttpServletResponse getResponse() {
        return response;
    }

    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }

    public void setSession(HttpSession session) {
        this.session = session;

    }

    public HttpSession getSession() {
        return session;
    }

    public void responseJsonData(String json) throws Exception {
        response.setContentType("application/json; charset=utf-8");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache, must-revalidate");
        response.setHeader("Pragma", "no-cache");
        response.getWriter().write(json);
        response.getWriter().flush();
        response.getWriter().close();
    }*/

}
